package com.community.controller;

import java.io.UnsupportedEncodingException;
import java.util.Map.Entry;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.poems.util.DetectFiveTonesIsPerfect;
import com.poems.util.Util;
import com.xit.poems.dto.FiveInformation;



@Controller
@RequestMapping("/ff")
public class DetectFiveForFiveController {

	@RequestMapping("/ffdetect")
	public String ffdetect(String poem,Model model) throws UnsupportedEncodingException{
	poem = new String(poem.getBytes("ISO-8859-1"), "utf-8");
		poem = poem.replaceAll("\\s", "");//去除换行
		//获取四个符号
		String[] fuhao = new String[4];
		fuhao[0] = poem.substring(5, 6);
		fuhao[1] = poem.substring(11, 12);
		fuhao[2] = poem.substring(17, 18);
		fuhao[3] = poem.substring(23, 24);

		poem = Util.delspecialsign(poem);//去除符号
		String poems1[] = poem.split("");
		String poems2[] = poem.split("");
		String poems3[] = poem.split("");
		DetectFiveTonesIsPerfect df = new DetectFiveTonesIsPerfect();
		
		FiveInformation ff1 = df.FiveClzyTonesIsPerfect(poem);
		FiveInformation ff2 = df.FivePsyTonesIsPerfect(poem);
		FiveInformation ff3 = df.FiveZhxyTonesIsPerfect(poem);
		String ff1error = "";
		String ff2error = "";
		String ff3error = "";
		
		String[] PoemsFormat1 = new String[20];
		String[] PoemsFormat2 = new String[20];
		String[] PoemsFormat3 = new String[20];
		
		for (int i = 0 ;i < ff1.getPoemsFormat().length-1;i++) {
			PoemsFormat1[i] = ff1.getPoemsFormat()[i+1];
		}
		for (int i = 0 ;i < ff2.getPoemsFormat().length-1;i++) {
			PoemsFormat2[i] = ff2.getPoemsFormat()[i+1];
		}
		for (int i = 0 ;i < ff3.getPoemsFormat().length-1;i++) {
			PoemsFormat3[i] = ff3.getPoemsFormat()[i+1];
		}
		
		String successMsg1 = "";
		String fistformat1 = "";
		if(ff1.getId() == 1){
			fistformat1 = "你输入的是一首<span style='color:red;'>五言绝句</span>,首句为（五言：<span style='color:red;'>仄起仄收</span>），其基本格式为<br>";
			successMsg1 = "你输入的是一首<span style='color:red;'>五言绝句</span>,首句为（五言：<span style='color:red;'>仄起仄收</span>）";
		}
		if(ff1.getId() == 2){
			fistformat1 = "你输入的是一首<span style='color:red;'>五言绝句</span>,首句为（五言：<span style='color:red;'>平起平收</span>），其基本格式为<br>";
			successMsg1 = "你输入的是一首<span style='color:red;'>五言绝句</span>,首句为（五言：<span style='color:red;'>平起平收</span>）";
		}
		if(ff1.getId() == 3){
			fistformat1 = "你输入的是一首<span style='color:red;'>五言绝句</span>,首句为（五言：<span style='color:red;'>平起仄收</span>），其基本格式为<br>";
			successMsg1 = "你输入的是一首<span style='color:red;'>五言绝句</span>,首句为（五言：<span style='color:red;'>平起仄收</span>）";
		}
		if(ff1.getId() == 4){
			fistformat1 = "你输入的是一首<span style='color:red;'>五言绝句</span>,首句为（五言：<span style='color:red;'>仄起平收</span>），其基本格式为<br>";
			successMsg1 = "你输入的是一首<span style='color:red;'>五言绝句</span>,首句为（五言：<span style='color:red;'>仄起平收</span>）";
		}
		
		if(ff1.getMap().size()>0){
			for (Entry<Integer, Object> s : ff1.getMap().entrySet()) {
				
				 poems1[s.getKey()] = "<span style='color:red;'>"+poem.substring(s.getKey()-1, s.getKey())+"</span>";
				 PoemsFormat1[s.getKey()-1] = "<span style='color:red;'>"+PoemsFormat1[s.getKey()-1]+"</span>";
				 
					//检测变格
				 int cc= FiveFourBiange(ff1.getId(),s.getKey(),PoemsFormat1);
				if(cc == 5){
					 ff1error = ff1error+"第"+s.getKey()
					 +"字<span style='color:red;'>"
					 +poem.substring(s.getKey()-1, s.getKey())
					 +"</span>应用：<span style='color:red;'>"+s.getValue()+"</span><br/>"; 

				}else{
					if(cc == 1){
						 ff1error = ff1error+"第"+s.getKey()
						 +"字<span style='color:red;'>"
						 +poem.substring(s.getKey()-1, s.getKey())
						 +"</span>应用：<span style='color:red;'>"+s.getValue()+"</span>,但符合出句四拗，对句三还的变格<br/>"; 

					}
					if (cc == 2) {
						 ff1error = ff1error+"第"+s.getKey()
						 +"字<span style='color:red;'>"
						 +poem.substring(s.getKey()-1, s.getKey())
						 +"</span>应用：<span style='color:red;'>"+s.getValue()+"</span>,但符合孤平本句救的变格<br/>"; 

					}
					if (cc == 3) {
						 ff1error = ff1error+"第"+s.getKey()
						 +"字<span style='color:red;'>"
						 +poem.substring(s.getKey()-1, s.getKey())
						 +"</span>应用：<span style='color:red;'>"+s.getValue()+"</span>,但符合本句3、4自救的变格<br/>"; 

					}
					if (cc == 4) {
						 ff1error = ff1error+"第"+s.getKey()
						 +"字<span style='color:red;'>"
						 +poem.substring(s.getKey()-1, s.getKey())
						 +"</span>应用：<span style='color:red;'>"+s.getValue()+"</span>,但符合本句3、4自救的变格<br/>"; 

					}
				}
			}
			ff1error = fistformat1 + ff1error;
		}else{
		
			model.addAttribute("ff1success", "<span style='color:red;'>恭喜您，"+successMsg1+",符合词林正韵！</span>");
		}
		
		String successMsg2 = "";
		String fistformat2 = "";
		if(ff2.getId() == 1){
			fistformat2 = "你输入的是一首<span style='color:red;'>五言绝句</span>,首句为（五言：<span style='color:red;'>仄起仄收</span>），其基本格式为<br>";
			successMsg2 = "你输入的是一首<span style='color:red;'>五言绝句</span>,首句为（五言：<span style='color:red;'>仄起仄收</span>）";
		}
		if(ff2.getId() == 2){
			fistformat2 = "你输入的是一首<span style='color:red;'>五言绝句</span>,首句为（五言：<span style='color:red;'>平起平收</span>），其基本格式为<br>";
			successMsg2 = "你输入的是一首<span style='color:red;'>五言绝句</span>,首句为（五言：<span style='color:red;'>平起平收</span>）";
		}
		if(ff2.getId() == 3){
			fistformat2 = "你输入的是一首<span style='color:red;'>五言绝句</span>,首句为（五言：<span style='color:red;'>平起仄收</span>），其基本格式为<br>";
			successMsg2 = "你输入的是一首<span style='color:red;'>五言绝句</span>,首句为（五言：<span style='color:red;'>平起仄收</span>）";
		}
		if(ff2.getId() == 4){
			fistformat2 = "你输入的是一首<span style='color:red;'>五言绝句</span>,首句为（五言：<span style='color:red;'>仄起平收</span>），其基本格式为<br>";
			successMsg2 = "你输入的是一首<span style='color:red;'>五言绝句</span>,首句为（五言：<span style='color:red;'>仄起平收</span>）";
		}
		if(ff2.getMap().size()>0){
			
			for (Entry<Integer, Object> s : ff2.getMap().entrySet()) {
				 poems2[s.getKey()] = "<span style='color:red;'>"+poem.substring(s.getKey()-1, s.getKey())+"</span>";
				 PoemsFormat2[s.getKey()-1] = "<span style='color:red;'>"+PoemsFormat2[s.getKey()-1]+"</span>";
				//检测变格
				 int cc= FiveFourBiange(ff2.getId(),s.getKey(),PoemsFormat2);
				if(cc == 5){
					 ff2error = ff2error+"第"+s.getKey()
					 +"字<span style='color:red;'>"
					 +poem.substring(s.getKey()-1, s.getKey())
					 +"</span>应用：<span style='color:red;'>"+s.getValue()+"</span><br/>"; 

				}else{
					if(cc == 1){
						 ff2error = ff2error+"第"+s.getKey()
						 +"字<span style='color:red;'>"
						 +poem.substring(s.getKey()-1, s.getKey())
						 +"</span>应用：<span style='color:red;'>"+s.getValue()+"</span>,但符合出句四拗，对句三还的变格<br/>"; 

					}
					if (cc == 2) {
						 ff2error = ff2error+"第"+s.getKey()
						 +"字<span style='color:red;'>"
						 +poem.substring(s.getKey()-1, s.getKey())
						 +"</span>应用：<span style='color:red;'>"+s.getValue()+"</span>,但符合孤平本句救的变格<br/>"; 

					}
					if (cc == 3) {
						 ff2error = ff2error+"第"+s.getKey()
						 +"字<span style='color:red;'>"
						 +poem.substring(s.getKey()-1, s.getKey())
						 +"</span>应用：<span style='color:red;'>"+s.getValue()+"</span>,但符合本句3、4自救的变格<br/>"; 

					}
					if (cc == 4) {
						 ff2error = ff2error+"第"+s.getKey()
						 +"字<span style='color:red;'>"
						 +poem.substring(s.getKey()-1, s.getKey())
						 +"</span>应用：<span style='color:red;'>"+s.getValue()+"</span>,但符合本句3、4自救的变格<br/>"; 

					}
				}
			}
			ff2error = fistformat2 + ff2error;
		}else{

			model.addAttribute("ff2success", "<span style='color:red;'>恭喜您，"+successMsg2+",符合平水韵！</span>");
		}
		String successMsg3 = "";
		String fistformat3 = "";
		if(ff3.getId() == 1){
			fistformat3 = "你输入的是一首<span style='color:red;'>五言绝句</span>,首句为（五言：<span style='color:red;'>仄起仄收</span>），其基本格式为<br>";
			successMsg3 = "你输入的是一首<span style='color:red;'>五言绝句</span>,首句为（五言：<span style='color:red;'>仄起仄收</span>）";
		}
		if(ff3.getId() == 2){
			fistformat3 = "你输入的是一首<span style='color:red;'>五言绝句</span>,首句为（五言：<span style='color:red;'>平起平收</span>），其基本格式为<br>";
			successMsg3 = "你输入的是一首<span style='color:red;'>五言绝句</span>,首句为（五言：<span style='color:red;'>平起平收</span>）";
		}
		if(ff3.getId() == 3){
			fistformat3 = "你输入的是一首<span style='color:red;'>五言绝句</span>,首句为（五言：<span style='color:red;'>平起仄收</span>），其基本格式为<br>";
			successMsg3 = "你输入的是一首<span style='color:red;'>五言绝句</span>,首句为（五言：<span style='color:red;'>平起仄收</span>）";
		}
		if(ff3.getId() == 4){
			fistformat3 = "你输入的是一首<span style='color:red;'>五言绝句</span>,首句为（五言：<span style='color:red;'>仄起平收</span>），其基本格式为<br>";
			successMsg3 = "你输入的是一首<span style='color:red;'>五言绝句</span>,首句为（五言：<span style='color:red;'>仄起平收</span>）";
		}
		if(ff3.getMap().size()>0){
			
			for (Entry<Integer, Object> s : ff3.getMap().entrySet()) {
				poems3[s.getKey()] = "<span style='color:red;'>"+poem.substring(s.getKey()-1, s.getKey())+"</span>";
				 PoemsFormat3[s.getKey()-1] = "<span style='color:red;'>"+PoemsFormat3[s.getKey()-1]+"</span>";
				//检测变格
				 int cc= FiveFourBiange(ff3.getId(),s.getKey(),PoemsFormat3);
				if(cc == 5){
					 ff3error = ff3error+"第"+s.getKey()
					 +"字<span style='color:red;'>"
					 +poem.substring(s.getKey()-1, s.getKey())
					 +"</span>应用：<span style='color:red;'>"+s.getValue()+"</span><br/>"; 

				}else{
					if(cc == 1){
						 ff3error = ff3error+"第"+s.getKey()
						 +"字<span style='color:red;'>"
						 +poem.substring(s.getKey()-1, s.getKey())
						 +"</span>应用：<span style='color:red;'>"+s.getValue()+"</span>,但符合出句四拗，对句三还的变格<br/>"; 

					}
					if (cc == 2) {
						 ff3error = ff3error+"第"+s.getKey()
						 +"字<span style='color:red;'>"
						 +poem.substring(s.getKey()-1, s.getKey())
						 +"</span>应用：<span style='color:red;'>"+s.getValue()+"</span>,但符合孤平本句救的变格<br/>"; 

					}
					if (cc == 3) {
						 ff3error = ff3error+"第"+s.getKey()
						 +"字<span style='color:red;'>"
						 +poem.substring(s.getKey()-1, s.getKey())
						 +"</span>应用：<span style='color:red;'>"+s.getValue()+"</span>,但符合本句3、4自救的变格<br/>"; 

					}
					if (cc == 4) {
						 ff3error = ff3error+"第"+s.getKey()
						 +"字<span style='color:red;'>"
						 +poem.substring(s.getKey()-1, s.getKey())
						 +"</span>应用：<span style='color:red;'>"+s.getValue()+"</span>,但符合本句3、4自救的变格<br/>"; 

					}
				}
			}
			ff3error = fistformat3 + ff3error;
		}else{
			model.addAttribute("ff3success", "<span style='color:red;'>恭喜您，"+successMsg3+",符合中华新韵！</span>");
		}
		
		
		String poemString1 = "";
		String poemString2 = "";
		String poemString3 = "";
		for(int i=1;i<poems1.length;i++){
			if(i==5 || i==10 || i==15 || i==20){
				poemString1 = poemString1 + poems1[i]+fuhao[i/5-1]+"<br/>";
				poemString2 = poemString2 + poems2[i]+fuhao[i/5-1]+"<br/>";
				poemString3 = poemString3 + poems3[i]+fuhao[i/5-1]+"<br/>";	
			}else{
				poemString1 = poemString1 + poems1[i];
				poemString2 = poemString2 + poems2[i];
				poemString3 = poemString3 + poems3[i];
			}
		}
	
		
		String PoemsFormatString1 = "";
		String PoemsFormatString2 = "";
		String PoemsFormatString3 = "";
		for(int i=0;i<PoemsFormat1.length;i++){
			if(i==4 || i==9 || i==14 || i==19){
				PoemsFormatString1 = PoemsFormatString1 + PoemsFormat1[i]+fuhao[i/4-1]+"<br/>";
				PoemsFormatString2 = PoemsFormatString2 + PoemsFormat2[i]+fuhao[i/4-1]+"<br/>";
				PoemsFormatString3 = PoemsFormatString3 + PoemsFormat3[i]+fuhao[i/4-1]+"<br/>";	
			}else{
				PoemsFormatString1 = PoemsFormatString1 + PoemsFormat1[i];
				PoemsFormatString2 = PoemsFormatString2 + PoemsFormat2[i];
				PoemsFormatString3 = PoemsFormatString3 + PoemsFormat3[i];
			}
		}

		
		model.addAttribute("ff1error", ff1error);
		model.addAttribute("ff2error", ff2error);
		model.addAttribute("ff3error", ff3error);
		model.addAttribute("poemString1", poemString1);
		model.addAttribute("poemString2", poemString2);
		model.addAttribute("poemString3", poemString3);
		model.addAttribute("PoemsFormatString1", PoemsFormatString1);
		model.addAttribute("PoemsFormatString2", PoemsFormatString2);
		model.addAttribute("PoemsFormatString3", PoemsFormatString3);		
		return "f_f_detext";
		
	}
	
	public int FiveFourBiange(int formatid,int key,String[] PoemsFormat){
		if (formatid == 1) {
			if (key == 4) {
				if(PoemsFormat[7].equals("平")){
					return 1;
				}
			}
		}
		if (formatid == 2) {
			if (key == 1) {
				if(PoemsFormat[2].equals("平")){
					return 2;
				}
			}
		}
		if (formatid == 3) {
			if (key == 4) {
				if(PoemsFormat[2].equals("仄")){
					return 3;
				}
			}
		}
		if (formatid == 4) {
			if (key == 3) {
				if(PoemsFormat[3].equals("仄")){
					return 4;
				}
			}
		}
		return 5;
	}
	
}
